package 设计模式.动态代理;

import java.lang.reflect.*;

/**
 * @Author: biubiu0002
 * @Date: 2021-09-09 18:26
 **/
public class ProxyTest {
    public static void main(String[] args) throws Exception {
        Class calculatorProxyClazz = Proxy.getProxyClass(Calculator.class.getClassLoader(), Calculator.class);
//        Constructor[] constructors = calculatorProxyClazz.getConstructors();
//        for (int i = 0; i < constructors.length; i++) {
//
//            System.out.println(constructors[i].getName());
//        }
//        System.out.println(constructors.length);
//        System.out.println( constructors[0].getParameterTypes());

        Constructor constructor = calculatorProxyClazz.getConstructor(InvocationHandler.class);
        Calculator calculatorProxyImpl = (Calculator) constructor.newInstance(new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //手动new一个impl对象
                CalculatorImpl calculatorImpl = new CalculatorImpl();
                Object res = method.invoke(calculatorImpl,args);
                return res;
            }
        });
        System.out.println(calculatorProxyImpl.add(2,3));

    }
}
